package com.timk.goserver.client.ui.screens;

import com.google.gwt.user.client.DeferredCommand;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.UIObject;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.timk.goserver.client.services.NoSessionException;
import com.timk.goserver.client.services.UserService;
import com.timk.goserver.client.ui.widgets.CenteredDialog;
import com.timk.goserver.client.ui.widgets.FocusCommand;
import com.timk.goserver.client.ui.widgets.StatusLabel;

/**
 * Dialog for changing password
 * @author TKington
 *
 */
public class ChangePasswordDialog extends CenteredDialog {
	final StatusLabel status;	// NOPMD - Explicit scoping
	private final PasswordTextBox passwordText;
	private final PasswordTextBox newPasswordText;
	private final PasswordTextBox confirmPasswordText;
	
	/**
	 * Creates a ChangePasswordDialog
	 * @param parent parent window
	 */
	public ChangePasswordDialog(UIObject parent) {
		super(parent);
		
		setText("Change Password");
		
		VerticalPanel mainPanel = new VerticalPanel();
		mainPanel.setWidth("250px");
		mainPanel.setSpacing(2);
		
		status = new StatusLabel();
		mainPanel.add(status);

		
		HorizontalPanel passwordPanel = new HorizontalPanel();
		mainPanel.add(passwordPanel);
		Label passwordLabel = new Label("Current Password:");
		passwordPanel.add(passwordLabel);
		passwordText = new PasswordTextBox();
		passwordPanel.add(passwordText);
		passwordPanel.setCellVerticalAlignment(passwordLabel, 
				HasVerticalAlignment.ALIGN_MIDDLE);
		passwordPanel.setWidth("100%");
		passwordLabel.setWidth("100%");
		passwordText.setWidth("100%");
		passwordPanel.setCellWidth(passwordLabel, "50%");
		passwordPanel.setCellWidth(passwordText, "50%");
		
		HorizontalPanel newPasswordPanel = new HorizontalPanel();
		mainPanel.add(newPasswordPanel);
		Label newPasswordLabel = new Label("New Password:");
		newPasswordPanel.add(newPasswordLabel);
		newPasswordText = new PasswordTextBox();
		newPasswordPanel.add(newPasswordText);
		newPasswordPanel.setCellVerticalAlignment(newPasswordLabel, 
				HasVerticalAlignment.ALIGN_MIDDLE);
		newPasswordPanel.setWidth("100%");
		newPasswordLabel.setWidth("100%");
		newPasswordText.setWidth("100%");
		newPasswordPanel.setCellWidth(newPasswordLabel, "50%");
		newPasswordPanel.setCellWidth(newPasswordText, "50%");
		
		HorizontalPanel confirmPasswordPanel = new HorizontalPanel();
		mainPanel.add(confirmPasswordPanel);
		Label confirmPasswordLabel = new Label("Confirm Password:");
		confirmPasswordPanel.add(confirmPasswordLabel);
		confirmPasswordText = new PasswordTextBox();
		confirmPasswordPanel.add(confirmPasswordText);
		confirmPasswordPanel.setCellVerticalAlignment(confirmPasswordLabel, 
				HasVerticalAlignment.ALIGN_MIDDLE);
		confirmPasswordPanel.setWidth("100%");
		confirmPasswordLabel.setWidth("100%");
		confirmPasswordText.setWidth("100%");
		confirmPasswordPanel.setCellWidth(confirmPasswordLabel, "50%");
		confirmPasswordPanel.setCellWidth(confirmPasswordText, "50%");
		
		HorizontalPanel buttonPanel = new HorizontalPanel();
		mainPanel.add(buttonPanel);
		mainPanel.setCellHorizontalAlignment(buttonPanel,
				HasHorizontalAlignment.ALIGN_CENTER);
		
		Button submitButton = new Button("Submit");
		submitButton.addClickListener(new ClickListener() {
			public void onClick(Widget sender) {
				onSubmit();
			}
		});
		submitButton.setWidth("60px");
		buttonPanel.add(submitButton);

		Button cancelButton = new Button("Cancel");
		cancelButton.addClickListener(new ClickListener() {
			public void onClick(Widget sender) {
				hide();
			}
		});
		cancelButton.setWidth("60px");
		buttonPanel.add(cancelButton);
		
		setWidget(mainPanel);
		
		DeferredCommand.add(new FocusCommand(passwordText));
	}
	
	void onSubmit() {
		String oldPassword = passwordText.getText();
		String newPassword = newPasswordText.getText();
		
		if(newPassword.equals(confirmPasswordText.getText())) {
			status.clear();
		} else {
			status.setInfo("Passwords do not match");
			return;
		}
		
		MainPanel.userService.changePassword(oldPassword,
				newPassword, new AsyncCallback() {
			public void onSuccess(Object result) {
				int err = ((Integer)result).intValue();
				
				switch(err) {
				case UserService.STATUS_SUCCESS:
					status.clear();
					hide();
					break;
				case UserService.STATUS_INCORRECT_PASSWORD:
					status.setInfo("Incorrect password");
					break;
				default:
					status.setError("Unrecognized error: " + err);
					break;
				}
			}
			
			public void onFailure(Throwable caught) {
				if(caught instanceof NoSessionException) {
					MainPanel.sessionExpired(caught.getMessage());
					return;
				}
				
				status.setError("Error changing password: " + caught.getMessage());
			}
		});
	}
}
